package cn.hallele.leinsteining.mc.infrastructure.flow.context;

import cn.hallele.leinsteining.mc.infrastructure.util.bean.CastUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yomahub.liteflow.exception.NullParamException;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;

/**
 * BaseContext
 *
 * @author anle5
 * @since 2024/8/4 23:13
 */
@Slf4j
public class BaseContext {
    private final ConcurrentHashMap<String, Object> DATA_MAP = new ConcurrentHashMap<>();

    private <T> void putDataMap(String key, T t) {
        if (ObjectUtil.isNull(t)) {
            throw new NullParamException("data can't accept null param");
        }
        DATA_MAP.put(key, t);
    }

    public boolean hasData(String key) {
        return DATA_MAP.containsKey(key);
    }

    public <T> T getData(String key) {
        return CastUtil.cast(DATA_MAP.get(key));
    }

    public <T> void setData(String key, T t) {
        putDataMap(key, t);
    }
}
